<?php

namespace App\Http\Controllers\Admin\Examine;

use App\Http\Controllers\Controller;
use App\Http\Requests\examine\ReimburseRequest;
use App\Models\examine\ConfigModel;
use App\Models\examine\RecordModel;
use App\Models\examine\ReimburseModel;
use Illuminate\Http\Request;

//报销管理
class Reimburse extends Controller
{
    use ReimburseRequest;

    /**
     * Notes:列表
     * User: xiangzi
     * Date: 2024/9/5
     * Time: 14:35
     * @return \Illuminate\Http\JsonResponse
     */
    public function list(Request $request) {
        $page = $request->input('page', 1);
        $limit = $request->input('limit', 20);
        $examine_no = $request->input('examine_no', '');
        $status = $request->input('status', '');
        $create_start = $request->input('create_start', '');
        $create_end = $request->input('create_end', '');

        $query = ReimburseModel::query()->with('create_admin');

        if($examine_no){
            $record_id = RecordModel::where('examine_no', $examine_no)->value('id');
            $query = $query->where('examine_record_id', $record_id);
        }

        if($status){
            $query = $query->where('status', $status);
        }

        if($create_start && $create_end){
            $query = $query->whereBetween('created_at', [$create_start, $create_end]);
        }

        $total = $query->count('id');
        $items = $query
            ->offset(($page-1) * $limit)
            ->limit($limit)
            ->orderBy('id', 'desc')
            ->get();

        $list = $items->map(function($item) {
            $res = [
                'id' => $item->id,
                'examine_no' => $item->record ? $item->record->examine_no : '',
                'amount' => $item->amount,
                'status' => $item->status,
                'status_text' => ReimburseModel::STATUS[$item->status],
                'is_urge' => $item->record ? $item->record->is_urge : 0,
                'created_at' => $item->created_at->toDateTimeString(),
            ];
            return $res;
        });

        return $this->success(['list' => $list, 'total' => $total]);
    }

    /**
     * Notes:添加
     * User: xiangzi
     * Date: 2024/9/5
     * Time: 14:35
     * @return \Illuminate\Http\JsonResponse
     */
    public function add(Request $request){
        $auth = $request->get('auth');
        $params = $request->all();

        $check_res = self::add_check($params);
        if($check_res->faild){
            return $this->error($check_res->err);
        }

        $model = new ReimburseModel();
        $model->amount = $params['amount'];
        $model->use_to = isset($params['use_to']) ? $params['use_to'] : '';
        $model->file = isset($params['file']) ? $params['file'] : '';
        $model->remarks = isset($params['remarks']) ? $params['remarks'] : '';
        $model->examine_record_id = (new Record())->add(1);
        $model->created_admin_id = $auth->id;
        $model->save();

        return $this->success();
    }

    public function update(Request $request, $id){
        $params = $request->all();

        $check_res = self::add_check($params);
        if($check_res->faild){
            return $this->error($check_res->err);
        }

        $model = ReimburseModel::find($id);
        if (!$model) {
            return $this->error('数据不存在');
        }

        $model->amount = $params['amount'];
        $model->use_to = isset($params['use_to']) ? $params['use_to'] : '';
        $model->file = isset($params['file']) ? $params['file'] : '';
        $model->remarks = isset($params['remarks']) ? $params['remarks'] : '';
        $model->save();

        return $this->success();
    }

    /**
     * Notes:撤回
     * User: xiangzi
     * Date: 2024/9/14
     * Time: 15:15
     * @return \Illuminate\Http\JsonResponse
     */
    public function cancel(Request $request, $id){
        $model = ReimburseModel::find($id);
        if (!$model) {
            return $this->error('数据不存在');
        }

        $record = RecordModel::find($model->examine_record_id);

        $record->status = 4;
        $record->cancel_time = date('Y-m-d H:i:s');
        $record->save();
        return $this->success();
    }

    /**
     * Notes:催促
     * User: xiangzi
     * Date: 2024/9/14
     * Time: 17:33
     * @return \Illuminate\Http\JsonResponse
     */
    public function urge($id){
        $model = ReimburseModel::find($id);
        if (!$model) {
            return $this->error('数据不存在');
        }

        $record = RecordModel::find($model->examine_record_id);
        $record->is_urge = 1;
        $record->save();
        return $this->success();
    }

    /**
     * Notes:详情
     * User: xiangzi
     * Date: 2024/9/18
     * Time: 10:26
     * @return \Illuminate\Http\JsonResponse
     */
    public function detail($id)
    {
        $model = ReimburseModel::with('create_admin')->find($id);
        if (!$model) {
            return $this->error('数据不存在');
        }

        $record = RecordModel::find($model->examine_record_id);

        $return = [
            'base_info' => [
                'examine_type' => ConfigModel::EXAMINE_TYPE[$record->examine_type],
                'submit_user' => $model->create_admin ? $model->create_admin->staff->name : '',
                'submit_time' => $model->created_at->toDateTimeString(),
                'examine_no' => $record->examine_no,
                'status' => $record->status,
                'status_text' => RecordModel::STATUS[$record->status],
                'cancel_time' => $record->cancel_time,
                'reason' => $record->reason
            ],
            'examine_info' => [
                'amount' => $model->amount,
                'use_to' => $model->use_to,
                'file' => $model->file,
                'remarks' => $model->remarks,
            ],
            'examine_process' => (new Record())->examine_info($model->examine_record_id),
            'copy_info' => (new Record())->copy_info($model->examine_record_id)
        ];

        return $this->success($return);
    }

}
